***generate targeted earnings moments
use "$temp/psid_long_cleaned", clear
keep if age<=54 //limit age range
su wages if age>=36

//get skill prices
merge m:1 statefips using "$temp/skill_prices_all", keep(match) nogen

//assign people to college based on mode:
bys uniqid: egen coll_mode = mode(coll)
drop if coll_mode == .

//moments: mean and variance of normalized earnings in the early part of life cycle,
//and mean/variance of growth rate from first to second part of life cycle
//here we'll allow moving

//normalize wages
//replace wages = wages if !coll //convert to real terms to account for oversampling of different locations
//replace wages = wages if coll //convert to real terms to account for oversampling of different locations
preserve
	use "$temp/wage_norm", clear
	su mean
	local norm `r(mean)'
restore
replace wages = wages/`norm' //normalize by mean earnings; 43,983.68
//replace wages = wages/skill_price_2000 if !coll //normalize by mean earnings; 43,983.68
//replace wages = wages/skill_price_coll_2000 if coll //normalize by mean earnings; 43,983.68
//su wages

//su wages if age>=36

//generate age groups and collapse
gen age_group = 1
replace age_group = 2 if age>=36 //older age groups
gen count = 1
set seed 1234

//begin the bootstrap
qui{
forval i = 1/1000{
	preserve
	
	if mod(`i', 20)==0{
		noi di as result "`i'"
	}
	
	bsample
	collapse (first) coll_mode (sum) count (mean) wages hours weight, by(uniqid age_group)
	drop if count<6 //drop if we don't get enough years
	gen hc = wages * (2080/hours) //hours normalization

	//drop if individual appears in only one age group
	duplicates tag uniqid, gen(dup)
	drop if dup==0 

	//convert to wide form again, in a sense
	sort uniqid age_group
	gen wages_old = .
	replace wages_old = wages[_n+1] if age_group[_n] == 1
	drop if age_group == 2
	drop dup

	//kill a few outliers that end up driving the distribution excessively
	drop if wages>6
	drop if wages_old>6
	//su wages wages_old [w=weight] if 

	//create moments
	su wages wages_old
	su wages [w=weight] if !coll //mean and variance of wages in early life cycle
	local mean_earn_ncoll `r(mean)'
	local sd_earn_ncoll `r(sd)'

	su wages [w=weight] if coll //mean and variance of wages in early life cycle
	local mean_earn_coll `r(mean)'
	local sd_earn_coll `r(sd)'

	//percentage growth
	gen growth = log(wages_old) - log(wages)
	su growth [w=weight] if !coll
	local mean_growth_ncoll `r(mean)'
	local sd_growth_ncoll `r(sd)'

	su growth [w=weight] if coll 
	local mean_growth_coll `r(mean)'
	local sd_growth_coll `r(sd)'

	su wages_old [w=weight] if !coll //mean and variance of wages in early life cycle
	local mean_earn_old_ncoll `r(mean)'
	local sd_earn_old_ncoll `r(sd)'

	su wages_old [w=weight] if coll //mean and variance of wages in early life cycle
	local mean_earn_old_coll `r(mean)'
	local sd_earn_old_coll `r(sd)'

	//generate targeted moment spreadsheet
	clear
	set obs 12
	gen moment = .
	replace moment = `mean_earn_ncoll' in 1
	replace moment = `sd_earn_ncoll' in 2
	replace moment = `mean_earn_coll' in 3
	replace moment = `sd_earn_coll' in 4
	replace moment = `mean_growth_ncoll' in 5
	replace moment = `sd_growth_ncoll' in 6
	replace moment = `mean_growth_coll' in 7
	replace moment = `sd_growth_coll' in 8
	replace moment = `mean_earn_old_ncoll' in 9
	replace moment = `sd_earn_old_ncoll' in 10
	replace moment = `mean_earn_old_coll' in 11
	replace moment = `sd_earn_old_coll' in 12

	xpose, clear
	save "$temp/boot/earnings_boot_`i'", replace
	restore
}
}

clear
forval i = 1/1000{
	append using "$temp/boot/earnings_boot_`i'"
}
export delimited "$dir/Model/boot/earnings_boot_sample.csv", novarn replace


//end of dofile
